<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>支付...</title>
</head>
<body>
<div>v11120923</div>
<script type="text/javascript">
    //禁止浏览器回退/返回
    XBack = {};
    (function (XBack) {
        XBack.STATE = 'x - back';
        XBack.element;
        XBack.onPopState = function (event) {
            event.state === XBack.STATE && XBack.fire();
            XBack.record(XBack.STATE); //初始化事件时，push一下
        };
        XBack.record = function (state) {
            history.pushState(state, null, location.href);
        };
        XBack.fire = function () {
            var event = document.createEvent('Events');
            event.initEvent(XBack.STATE, false, false);
            XBack.element.dispatchEvent(event);
        };
        XBack.listen = function (listener) {
            XBack.element.addEventListener(XBack.STATE, listener, false);
        };
        XBack.init = function () {
            XBack.element = document.createElement('span');
            window.addEventListener('popstate', XBack.onPopState);
            XBack.record(XBack.STATE);
        };
    })(XBack); // 引入这段js文件
    XBack.init();
    XBack.listen(function () {
    });
    let ua = window.navigator.userAgent.toLowerCase()
    if (ua.match(/MicroMessenger/i) == 'micromessenger') {
        if (typeof WeixinJSBridge == "undefined") {
            if (document.addEventListener) {
                document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
            } else if (document.attachEvent) {
                document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
            }
        }
    } else if (ua.match(/AliApp/i) == 'aliapp') {
        if (!window.AlipayJSBridge) {
            // 如果没有注入则监听注入的事件
            document.addEventListener('AlipayJSBridgeReady', callback, false);
        }
    }
    var type = '';

    var payInfo = {
        appId: '',
        timeStamp: '',
        nonceStr: '',
        package: '',
        signType: '',
        paySign: '',
        tradeNO:''
    }
    var reUrl = '';// 成功页面
    var canUrl = '';// 取消页面
    var orderNo = '';
    window.onload = function () {

        // let en = window.btoa('china is so nb') // 编码
        // console.log(en)
        // let de = window.atob("Y2hpbmEgaXMgc28gbmI=") // 解码
        // console.log(de)
        let ua = window.navigator.userAgent.toLowerCase();
        let browse;
        // @ts-ignore
        if (ua.match(/MicroMessenger/i) == 'micromessenger') {
            browse = 'wx';
            // @ts-ignore
        }else if (ua.match(/AliApp/i) == 'aliapp') {
            browse = 'ali';
        } else {
            window.location.href = "http://www.baidu.com";
            return;
        }
        let url = document.URL;
        console.log(url);
        let arr = url.split('?');
        let param = String(arr[1]).split('&');
        console.log(param)
        for (let item of param) {
            let par = item.indexOf("=");
            let prefix = item.substring(0, par);
            let suffix = item.substring(par + 1);
            if (prefix === 'appId') {
                payInfo.appId = suffix;
            }
            if (prefix === 'timeStamp') {
                payInfo.timeStamp = suffix;
            }
            if (prefix === 'nonceStr') {
                payInfo.nonceStr = suffix;
            }
            if (prefix === 'package') {
                payInfo.package = suffix;
            }
            if (prefix === 'signType') {
                payInfo.signType = suffix;
            }
            if (prefix === 'paySign') {
                payInfo.paySign = suffix;
            }
            if (prefix === 'type') {
                type = suffix;
            }
            if (prefix === 'reUrl') {
                reUrl = window.decodeURIComponent(suffix);
            }
            if (prefix === 'canUrl') {
                canUrl = window.decodeURIComponent(suffix);
            }
            if (prefix === 'orderNo') {
                orderNo = suffix;
            }
            if (prefix === 'tradeNO') {
                payInfo.tradeNO = suffix;
            }
        }
        if (ua.match(/MicroMessenger/i) == 'micromessenger') {
            onBridgeReady();
            // @ts-ignore
        } else if (ua.match(/AliApp/i) == 'aliapp') {
            onBridgeReadyAli();
        }
    };

    function onBridgeReady() {
        WeixinJSBridge.invoke(
            'getBrandWCPayRequest', {
                "appId": decodeURIComponent(payInfo.appId),
                "timeStamp": decodeURIComponent(payInfo.timeStamp),
                "nonceStr": decodeURIComponent(payInfo.nonceStr),
                "package": decodeURIComponent(payInfo.package),
                "signType": decodeURIComponent(payInfo.signType),
                "paySign": decodeURIComponent(payInfo.paySign)
            },
            function (res) {
                if (res.err_msg == "get_brand_wcpay_request:ok") {
                    window.location.href = reUrl;
                } else if (res.err_msg == "get_brand_wcpay_request:cancel") {
                    window.location.href = canUrl;
                } else {
                    window.location.href = canUrl;
                }
            });
    }
    function onBridgeReadyAli() {
        AlipayJSBridge.call("tradePay", {
            tradeNO: decodeURIComponent(payInfo.tradeNO)
        }, function(result) {
            if (result.resultCode == '9000') {
                //支付成功
                window.location.href = reUrl;
            } else if (result.resultCode == '6001' || result.resultCode == '6002' || result.resultCode == '4000') {
                window.location.href = canUrl;
            } else {
                window.location.href = canUrl;
            }
        });
    }


</script>
</body>
</html>